import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:good_good_study/common/network/net_cancel.dart';
import 'package:good_good_study/common/utils/route_util.dart';
import 'package:good_good_study/common/widget/constant.dart';
import 'package:good_good_study/provider/collect_provider.dart';
import 'package:good_good_study/provider/home_provider.dart';
import 'package:provider/provider.dart';
import 'package:good_good_study/entity/collect_list_entity.dart' as Collect;

class CollectPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _CollectPageState();
  }
}

class _CollectPageState extends State<CollectPage> with NetCancel {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: SizedBox(),
        title: Text("收藏"),
        centerTitle: true,
      ),
      body: Consumer<CollectProvider>(
        builder: (context, collectProvider, child) {
          if (collectProvider.nextPageIndex == 0) {
            collectProvider.getCollectListPage(this, page: 0);
          }
          return collectProvider.collectList.isEmpty
              ? GestureDetector(
                  onTap: () {
                    collectProvider.getCollectListPage(this, page: 0);
                  },
                  child: buildPageEmpty(),
                )
              : Container(
                  child: EasyRefresh(
                    header: MaterialHeader(),
                    footer: MaterialFooter(),
                    onRefresh: () {
                      collectProvider.getCollectListPage(this, page: 0);
                      return Future.value(null);
                    },
                    onLoad: () {
                      collectProvider.getCollectListPage(this);
                      return Future.value(null);
                    },
                    child: ListView.builder(
                      itemBuilder: (context, pos) {
                        return buildListItem(
                            context, pos, collectProvider.collectList[pos]);
                      },
                      itemCount: collectProvider.collectList.length,
                    ),
                  ),
                );
        },
      ),
    );
  }

  Widget buildListItem(BuildContext context, int pos, Collect.Datas item) {
    return GestureDetector(
      onTap: () {
        RouteUtil.toWebView(context, item.title, item.link);
      },
      child: Container(
        alignment: Alignment.center,
        height: 60,
        child: Row(
          children: [
            SizedBox(
              width: 30,
            ),
            Expanded(
              child: Text(
                item.title,
                style: TextStyle(color: Colors.red),
              ),
            ),
            SizedBox(
              width: 30,
            ),
          ],
        ),
      ),
    );
  }
}
